package com.anrong.wulansdk.sdk;

import com.anrong.wulansdk.sdk.constant.Constant;
import com.anrong.wulansdk.sdk.utils.Utils;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.TextUtils;

/**
 * 配置类
 * @author cxy
 *
 */
public class WLConfig {
	private Application app;
	private SendMode sendMode;
	private long sendTime;
	private boolean showLog;

	private WLConfig(Builder b) {
		this.app = b.app;
		this.sendMode = b.sendMode;
		this.sendTime = b.sendTime;
		this.showLog = b.showLog;
		
		
		//判断如果代码没有设置appkey则从清单配置文件中提取appkey
		if (TextUtils.isEmpty(b.appKey)) {
			AppKey.appKey = getAppKey();
		}else {
			AppKey.appKey = b.appKey;
		}
		
		//当前测试以应用的包名作为appkey
//		AppKey.appKey = app.getPackageName();
	}

	/**
	 * 获取清单配置文件中设置的appkey
	 * @return
	 */
	private String getAppKey() {
		String key = "";
		try {
			key = app.getPackageManager().getApplicationInfo(app.getPackageName(), PackageManager.GET_META_DATA).metaData.getString("wl_appkey");
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return key;
	}
	
	public Application getApp() {
		return app;
	}

	/**
	 * 获取发送策略
	 * @return
	 */
	public SendMode getSendMode(){
		return sendMode;
	}
	/**
	 * 获取按时间发送时的时间间隔
	 * @return
	 */
	public long getSendTime(){
		return sendTime;
	}
	/**
	 * 获取是否显示log
	 * @return
	 */
	public boolean getshowLog(){
		return showLog;
	}
	
	
	public static class Builder{
		
		private Application app;
		private SendMode sendMode = SendMode.SEND_MODE_TIME;//发送策略，默认时间间隔发送
		private long sendTime = 5*60*1000;//按时间间隔发送的时间间隔，默认5分钟
		private boolean showLog = false;//是否显示log，默认false
		private String appKey = "";//用户到后台服务器注册获得
		private boolean run = true;//开关
		
		public Builder(Application app) {
			this.app = app;
		}
		
		/**
		 * 发送策略
		 * 1.启动时发送
		 * 2.按时间间隔发送
		 */
		private Builder setSendMode(SendMode sendMode){
			this.sendMode = sendMode;
			return this;
		}
		private Builder setSendTime(long sendTime){
			this.sendTime = sendTime;
			return this;
		}
		private Builder setShowLog(boolean showLog){
			this.showLog = showLog;
			return this;
		}
		public Builder setAppKey(String appKey){
			this.appKey = appKey;
			return this;
		}
		private Builder setRun(boolean run) {
			this.run = run;
			return this;
		}

		public WLConfig build(){
			return new WLConfig(this);
		}
	}
	
	/**
	 * 发送策略
	 * @author cxy
	 *
	 */
	public enum SendMode{
		SEND_MODE_TIME;
	}
	
}
